package com.jiehoo.elove.robot;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import rath.msnm.MSNMessenger;
import rath.msnm.SwitchboardSession;

public class SessionProcesser extends Thread {
	private static Log log = LogFactory.getLog(SessionProcesser.class);

	private List<SwitchboardSession> sessionList = new ArrayList<SwitchboardSession>();

	public SessionProcesser(MSNMessenger messanger) {
	}

	public void addSession(SwitchboardSession session) {
		if (!sessionList.contains(session)) {
			session.setTimeout(RobotServer.timeout);
			sessionList.add(session);
		}
	}

	public void removeSession(SwitchboardSession session) {
		if (sessionList.contains(session)) {
			sessionList.remove(session);
		}
	}

	public void run() {
		while (true) {
			try {
				for (int i = 0; i < sessionList.size(); i++) {
					SwitchboardSession session = (SwitchboardSession) sessionList
							.get(i);
					session.sendTypingMessage(RobotServer.typing);
					log.debug("send typing message!");
				}
				Thread.sleep(10000);
			} catch (IOException e) {
				log.error("Send typing message error!", e);
			} catch (InterruptedException e) {

			}
		}
	}
}